package com.simpletour.qa.common.http;

import lombok.Getter;
import lombok.Setter;

/**
 * RESTful api 返回结果
 * @author zhaohw
 * @date 2018-06-19 15:33
 */
@Setter
@Getter
public class ObjectRestResponse<T>{

    private int code;

    private String error;

    private String msg;

    private T result;

    private ObjectRestResponse(ObjectRestResponse.Builder builder){
        this.code = builder.code;
        this.error = builder.error;
        this.msg = builder.msg;
        this.result = (T) builder.result;
    }

    public static final class Builder<K>{

        private int code;

        private String error;

        private String msg;

        private K result;

        private Builder() {
            this.code = 0;
            this.error = "";
            this.msg = "";
            this.result = null;
        }

        public ObjectRestResponse.Builder code(int code) {
            this.code = code;
            return this;
        }

        public ObjectRestResponse.Builder code(ResponseCode codeEnum) {
            this.code = codeEnum.code;
            this.error = codeEnum.error;
            return this;
        }

        public ObjectRestResponse.Builder error(String error) {
            this.error = error;
            return this;
        }

        public ObjectRestResponse.Builder msg(String msg) {
            this.msg = msg;
            return this;
        }

        public ObjectRestResponse.Builder result(K result) {
            this.result = result;
            return this;
        }

        public ObjectRestResponse build() {
            return new ObjectRestResponse(this);
        }
    }

    public static ObjectRestResponse.Builder builder() {
        return new ObjectRestResponse.Builder();
    }


}

